package com.chaopei.day11;

import sun.net.www.protocol.https.HttpsURLConnectionImpl;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executor;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author Corday 天涯
 * @Date 2022/3/17 9:46
 * 使用定时器实现网络抓虫功能
 */
public class MyTimer {
    public static void scratchPages() {
        Timer timer = null;
        TimerTask tt = null;
        //传入一个网络地址
        String url = "https://www.baidu.com";
        timer = new Timer();
        //传参
        tt = new MyTimerTask(url, timer);
        //如果设置从一个定点的时间开始执行定时任务
        //比如：从2022年3月17日11:00开始，每隔5小时执行一次
        //Date date = new Date(2022-1900,2,17,10,56,10);
        Date date = null;
        Calendar cd = Calendar.getInstance();
        cd.set(2022, 2, 17, 11, 39, 0);
        date = cd.getTime();
        timer.schedule(tt, 3000);
        //定时方式有四种
        //退出或结束定时器线程：timer.cancel();
    }//scrat

    //自定义一个函数完成定时设置
    public long createDelay(int... num) {
        return 0;
    }
}

//自定义一个定时器任务类
class MyTimerTask extends TimerTask {
    private String url;
    //声明一个定时器对象
    private Timer timer;

    public MyTimerTask() {
    }

    public MyTimerTask(String url, Timer timer) {
        this.url = url;
        this.timer = timer;
    }

    @Override
    public void run() {//完成具体的定时操作
        spider();
    }

    //    实现网络抓虫功能
    public void spider() {
        //声明对象
        InputStream in = null;
        PrintWriter pw = null;
        HttpsURLConnectionImpl huc = null;
        BufferedReader br = null;
        URL url = null;
        //创建对象
        try {
            url = new URL(this.url);
            huc = (HttpsURLConnectionImpl) url.openConnection();
            //网络连接准备工作
            huc.setRequestMethod("GET");
            huc.setReadTimeout(2000);
            huc.setConnectTimeout(2000);
            //打开网络连接
            huc.connect();
            in = huc.getInputStream();
            //将字节流转换成字符流
            br = new BufferedReader(new InputStreamReader(in));
            //创建打印字符输出流对象，保存数据到磁盘
            pw = new PrintWriter(new FileWriter("c:/temp/spider.txt", true));
            String temp = "";
            //写正则
            String regx = "https://\\w+\\.\\w+\\.\\w+(\\.\\w+)?";
            Pattern p = Pattern.compile(regx);
            while ((temp = br.readLine()) != null) {
                Matcher matcher = p.matcher(temp);
                while (matcher.find()) {//hasNext
                    //从正则缓存对象matcher中提取所有网址
                    String u = matcher.group();
                    pw.println(u);
                }//while
            }
            //关闭流
            in.close();
            pw.close();
            //断开网络连接
            huc.disconnect();
            //结束定时线程,没问题
            timer.cancel();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

//测试一下
class TestMyTimer {
    public static void main(String[] args) {
        MyTimer.scratchPages();
    }//main
}